package us.mmapp;

import java.util.ArrayList;

import us.mmapp.util.DBOpenHelper;
import us.mmapp.util.ThumbnailUtil;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TableRow;

public class ManageActivity extends Activity implements OnClickListener
{
	TableRow[] rows;
	Button[] buttons;
	int[] files;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.manage);

	}

	public void onClick(View arg0)
	{
		for (int i = 0; i < files.length; i++)
		{
			if (arg0.equals(buttons[i]))
			{
				Intent intent = new Intent();
				intent.setClass(this, CommentActivity.class);
				Bundle bundle = new Bundle();
				bundle.putInt("keyword", files[i]);
				intent.putExtras(bundle);
				startActivity(intent);
			}
			else if (arg0.equals(rows[i]))
			{
				Intent intent = new Intent();// intent is like the envelop
				intent.setClass(this, InfoActivity.class);// arg1 is the current
				// class and arg2 is
				// the class u wanna
				// call
				Bundle bundle = new Bundle(); // bundle is like the letter
				// bundle.put
				bundle.putInt("keyword", files[i]); // arg1 is the keyword of
				// the txt, arg2 is the txt
				bundle.putBoolean("new", false);
				intent.putExtras(bundle);// actually it's bundle who carries the
				// content u wanna pass
				startActivity(intent);
			}
		}
	}
	@Override
	public void onResume()
	{
		super.onResume();
		setTable();
	}
	public int[] getLocalMedia()
	{
		SharedPreferences settings = getSharedPreferences("LOGIN", 0);
		DBOpenHelper data = new DBOpenHelper(this, "mmAppDB", DBOpenHelper.VERSION);
		SQLiteDatabase db = data.getReadableDatabase();
		Cursor dbObjects = db.rawQuery("SELECT " + DBOpenHelper.SID + " FROM OBJECTS WHERE UID=" + settings.getLong("uid", 0), null);
		ArrayList<Integer> fullList = new ArrayList<Integer>();
		while (dbObjects.moveToNext())
		{
			fullList.add(new Integer(dbObjects.getInt(0)));
		}
		int[] fullList1 = new int[fullList.size()];
		for (int i = 0; i < fullList.size(); i++)
		{
			fullList1[i] = fullList.get(i).intValue();
		}
		dbObjects.close();

		db.close();
		return fullList1;
	}

	void setTable()
	{
		TableLayout tl = (TableLayout) findViewById(R.id.tableView);
		tl.setShrinkAllColumns(false);
		tl.setStretchAllColumns(false);
		tl.removeAllViews();
		files = getLocalMedia();
		rows = new TableRow[files.length];
		buttons = new Button[files.length];
		for (int i = 0; i < files.length; i++)
		{
			rows[i] = new TableRow(this);
			buttons[i] = new Button(this);
			buttons[i].setText("View");
			rows[i].addView(buttons[i]);

			ImageView imageView = new ImageView(this);
			imageView.setAdjustViewBounds(true);
			imageView.setMaxHeight(200);
			imageView.setMinimumHeight(200);
			imageView.setPadding(10, 10, 10, 10);
			ThumbnailUtil.setImageView(this, files[i], imageView, true);
			rows[i].addView(imageView);
			tl.addView(rows[i]);
			rows[i].setOnClickListener(this);
			buttons[i].setOnClickListener(this);
		}
	}

}
